# -*- coding:utf-8 -*-
import web
from config import settings
from config import util
import datetime
import authbase

render = settings.render
db = settings.db

class DayChart(authbase.AuthBase):
    def GET(self):
        stations_input = web.input(stations=[])
        #print "stations_input:%s"%stations_input
        stations = stations_input.get('stations',None)
        where_str=" 1=1 "
        if stations:
            where_str=where_str+" and site in ('"+"','".join(stations)+"')"
            selected_stations=stations
        else:
            selected_stations=None

        data_type_input = web.input(data_type=[])
        #print "data_type_input:%s"%data_type_input
        data_type_names = data_type_input.get('data_type',None)
        #print "data_types:%s"%data_type_names

        i = web.input()
        print i
        start_date = i.get('dateStart', None)
        #默认最近一个月#上月1号
        if start_date is None:
            last_month_start=datetime.date(datetime.date.today().year,datetime.date.today().month-1,1)
            start_date= datetime.datetime.strftime(last_month_start, "%Y-%m-%d")
        where_str=where_str+" and collection_date >= '"+start_date+"'"
        selected_start_date=start_date

        end_date = i.get('dateEnd', None)
        if end_date is None:
            end_date=datetime.datetime.strftime(datetime.date.today(), "%Y-%m-%d")
        where_str=where_str+" and collection_date <= '"+end_date+"'"
        selected_end_date=end_date

        #将查询结果转为list，可以多次遍历
        data_list =list(db.query("SELECT * FROM v_site_day_data  where "+where_str))
        stations=list(db.select('stations'))
        line_name_dict,chart_data_array=self.create_chart_data(data_type_names,stations,data_list)
        return render.charts.day_chart(data_list=data_list,line_name_dict=line_name_dict,chart_data_array=util.two_array_to_utf_string(chart_data_array), stations=stations,\
                                      selected_stations=util.array_to_utf_string(selected_stations),selected_start_date=selected_start_date,
                                      selected_end_date=selected_end_date,data_type_names=data_type_names,\
                                      data_type_titles=util.create_field_title_by_avg_name(data_type_names))


    def create_chart_data(self,data_types,stations,data_list):
        #每个监测站生成一条曲线
        #按监测站、参数类型的组合方式来生成曲线
        data_array=[]
        line_dict={}
        i=0
        #从data list中取日期列表，去重并排序，,必须要考虑日期缺漏的情况，不同监测站的日期不一致怎么办
        x_date_array=util.get_date_sort_array_from_list(data_list)

        for station in stations:
            #先判断数据集里面是否有该监测站
            station_exist=util.is_station_in_data_list(station.name,data_list)
            if station_exist==False:
                print "%s not in data_list"%station.name
                continue
            short_name=station.name[0:2]
            for data_type in data_types:
                title=short_name+util.data_type_title_dict[data_type]
                row_array=[]
                #生成某站点 在指定的日期范围内 某种监测类型的数据array
                for item in x_date_array:
                    #在data_list中查找监测站名称、日期符合的记录，找到后返回指定数据列的值,没找到返回‘’
                    #print "call util.query_site_date_type_value(station,item,data_type,data_list)(%s,%s,%s)"%(station.name,item,data_type)
                    result_value=util.query_site_date_type_value(station.name,item,data_type,data_list)
                    row_array.append(result_value)

                #如果全部都是‘’空值，说明没有这个监测站的数据
                if len(row_array)==0:
                    continue
                #曲线编号和中文名称对应
                line_dict[data_type+"_%02d"%i]=title
                row_array.insert(0,title)
                data_array.append(row_array)
                i=i+1
        #为x轴数据加上开头字符串
        x_date_array.insert(0,"x_time_array")
        line_dict["line00"]="x_time_array"
        data_array.insert(0,x_date_array)
        #print data_array
        return line_dict,data_array

class MonthChart(authbase.AuthBase):
    def GET(self):
        stations_input = web.input(stations=[])
        #print "stations_input:%s"%stations_input
        stations = stations_input.get('stations',None)
        where_str=" 1=1 "
        if stations:
            where_str=where_str+" and site in ('"+"','".join(stations)+"')"
            selected_stations=stations
        else:
            selected_stations=None

        data_type_input = web.input(data_type=[])
        #print "data_type_input:%s"%data_type_input
        data_type_names = data_type_input.get('data_type',None)
        #print "data_types:%s"%data_type_names

        i = web.input()
        print i
        start_date = i.get('dateStart', None)
        if start_date:
            where_str=where_str+" and collection_month >= '"+start_date+"'"
            selected_start_date=start_date
        else:
            selected_start_date=None

        end_date = i.get('dateEnd', None)
        if end_date:
            where_str=where_str+" and collection_month <= '"+end_date+"'"
            selected_end_date=end_date
        else:
            selected_end_date=None
        #将查询结果转为list，可以多次遍历
        data_list =list(db.query("SELECT * FROM v_site_month_data  where "+where_str))
        stations=list(db.select('stations'))
        line_name_dict,chart_data_array=self.create_chart_data(data_type_names,stations,data_list)
        return render.charts.month_chart(data_list=data_list,line_name_dict=line_name_dict,chart_data_array=util.two_array_to_utf_string(chart_data_array), stations=stations,\
                                      selected_stations=util.array_to_utf_string(selected_stations),selected_start_date=selected_start_date,
                                      selected_end_date=selected_end_date,data_type_names=data_type_names,\
                                      data_type_titles=util.create_field_title_by_avg_name(data_type_names))

    def create_chart_data(self,data_types,stations,data_list):
        #每个监测站生成一条曲线
        #按监测站、参数类型的组合方式来生成曲线
        data_array=[]
        line_dict={}
        i=0
        #从data list中取日期列表，去重并排序，,必须要考虑日期缺漏的情况，不同监测站的日期不一致怎么办
        x_date_array=util.get_month_sort_array_from_list(data_list)

        for station in stations:
            #先判断数据集里面是否有该监测站
            station_exist=util.is_station_in_data_list(station.name,data_list)
            if station_exist==False:
                print "%s not in data_list"%station.name
                continue
            short_name=station.name[0:2]
            for data_type in data_types:
                title=short_name+util.data_type_title_dict[data_type]
                row_array=[]
                #生成某站点 在指定的日期范围内 某种监测类型的数据array
                for item in x_date_array:
                    #在data_list中查找监测站名称、日期符合的记录，找到后返回指定数据列的值,没找到返回‘’
                    #print "call util.query_site_date_type_value(station,item,data_type,data_list)(%s,%s,%s)"%(station.name,item,data_type)
                    result_value=util.query_site_month_type_value(station.name,item,data_type,data_list)
                    row_array.append(result_value)

                #如果全部都是‘’空值，说明没有这个监测站的数据
                if len(row_array)==0:
                    continue
                #曲线编号和中文名称对应
                line_dict[data_type+"_%02d"%i]=title
                row_array.insert(0,title)
                data_array.append(row_array)
                i=i+1
        #为x轴数据加上开头字符串
        x_date_array.insert(0,"x_time_array")
        line_dict["line00"]="x_time_array"
        data_array.insert(0,x_date_array)
        #print data_array
        return line_dict,data_array
